################################################################################ 
#
# Counterspeech encouraging users to adopt the perspective of minority 
# groups reduces hate speech and its amplification on social media
#
# Tab C4
#
################################################################################ 


rm(list = ls())

# Set to save results
setsave = T

################################################################################ 
#  LIBRARIES
################################################################################ 

library(dplyr)
library(estimatr)
library(xtable)

################################################################################ 
#   DATA AND FOLDER
################################################################################ 

wd = ''
wd_res = paste0(wd, '/results')
wd_data =  paste0(wd, '/data')


data = read.csv(paste0(wd_data, '/dataset_analysis.csv'))

data$orig_tweet_deleted[(data$user_deleted == "yes")] = 1
data$total_tweets_post[(data$user_deleted == "yes")] = 0
data$tox_dummy_p_key_num[(data$user_deleted == "yes")] = 0
data$tox_dummy_p_key[(data$user_deleted == "yes")] = 0


################################################################################ 
#   VARIABLES
################################################################################

outvars = c('orig_tweet_deleted', 'total_tweets_post',
                'tox_dummy_p_key_num', 'tox_dummy_p_key')

outvars_name = c('Xenophobic Tweet Deleted', '# Total Tweets',
                     '# Xenophobic Tweets', 'Xenophobic Tweet Share')

treatments = c('empathy', 'traditional', 'analogical', 'perspective_getting', 'disapproval')
treatments_lab = c( 'Empathy', 'Traditional Perspective Taking',  'Analogical Perspective Taking',
                    'Perspective Getting', 'Disapproval')

# Controls in the PAP
data$account_age_cuts = as.factor(cut(data$account_age, breaks=5))
controls = c("account_age_cuts", 'tox_num_pre', "followers_count",
             "pre_treat_tweet_length", "total_tweets_pre", "friends_count")

################################################################################ 
#   Scale variables
################################################################################ 

data[c(outvars)] = lapply(data[c(outvars)], scale)


################################################################################ 
#   Run the models with and without controls
################################################################################ 


MODELS = list()

n = 1

for (j in 1:length(outvars)){
  for (i in 1:length(treatments)){
    
    # drop missings
    droper = union(which(is.na(data[outvars[j]])), which(data[outvars[j]]==Inf))
    
    # keep the right treatment and control group
    keeper = union(which(data[treatments[i]] == 1),  which(data$group_number == 5)) 
    keeper = setdiff(keeper, droper)
    
    # select rows
    y = data[keeper, outvars[j]]
    d = data[,treatments[i]][keeper]
    ctr = data[keeper, controls]
    
    # regression
    MODELS[[n]] = lm_robust(y ~ d, se_type='HC3')

    print('Done with: ')
    print(n)
    
    n = n+1
    
  }}



################################################################################ 
#   Save results in a container
################################################################################ 


make_container = function(list_to_be_transformed, outvars, outvars_name){
  MOD = list_to_be_transformed
  
  coef = sapply(MOD, function(m) round(summary(m)$coefficients[2, 1], 3))
  se = sapply(MOD, function(m) round(summary(m)$coefficients[2, 2], 3))
  pval = sapply(MOD, function(m) round(summary(m)$coefficients[2, 4], 3))
  sample = sapply(MOD, function(m) nobs(m))
  
  container = cbind.data.frame(yvar = rep(outvars, each=5),
                              yvar_name = rep(outvars_name, each=5),
                               treat = rep(treatments, time=length(outvars)),
                               treatments_lab = rep(treatments_lab, time=length(outvars)),
                               coef = coef, se=se, pval=pval, 
                               samplesize = sample
  )
  return(container)
}


container_unc = make_container(MODELS, outvars, outvars_name)

if (setsave){write.csv(container_unc, paste0(wd_res, '/tables/main_result_wo_controls_attr.csv'), row.names = F)}
